/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.coupon.entity;

import cn.icanci.coupon.model.Coupon;
import cn.icanci.coupon.model.CouponRecord;
import cn.icanci.coupon.repository.CouponRepository;
import cn.icanci.coupon.utils.CouponUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.google.common.collect.Lists;

/**
 * 优惠券实体
 *
 * @author icanci
 * @date Created in 2021/05/01 10:01
 * @version 1.0
 */
@SuppressWarnings("all")
@Data
@Slf4j
public class CouponEntity extends Coupon {
    /**
     * 仓储
     */
    private CouponRepository   couponRepository;

    public CouponEntity(CouponRepository couponRepository) {
        this.couponRepository = couponRepository;
    }

    /**
     * 用户的所有的优惠券
     * @param userId 用户id
     * @return 返回优惠券
     */
    private CouponRecord userRecord(Integer userId) {
        return couponRepository.findRecord(this.getCouponNumber(), userId);
    }

    /**
     * 更新用户的信息
     * @param userId 用户id
     * @return 返回影响的行数
     */
    public int inertOneUserRecord(Integer userId) {
        return couponRepository.inertOneUserRecord(this, userId);
    }

    /**
     * 根据优惠券唯一键查询
     * @param userId 用户id
     * @return 返回查询的结果
     */
    public List<CouponRecord> findRecordByUserId(Integer userId) {
        return couponRepository.findRecordByUserId(userId);
    }

    /**
     * 保存一批优惠券
     * @param couponEntity 优惠券实体
     */
    public void saveCoupon(CouponEntity couponEntity) {
        Integer totalNum = couponEntity.getTotalNum();
        List<Coupon> couponList = Lists.newArrayList();
        // 处理数据
        log.info("数据处理中，{}", couponEntity);
        String uniqueKey = CouponUtils.getUniqueKey();
        for (Integer i = 0; i < totalNum; i++) {
            Coupon coupon = new Coupon();
            BeanUtils.copyProperties(couponEntity, coupon);
            String couponNumber = CouponUtils.getUniqueKeyCouponNumber(couponEntity, i);
            coupon.setCouponNumber(couponNumber);
            coupon.setUniqueKey(uniqueKey);
            log.info("数据处理中，插入一条：{}", coupon);
            couponList.add(coupon);
        }
        int result = couponRepository.saveCoupons(couponList);
    }

    /**
     * 更新状态
     */
    public int updateStatusOrder() {
        this.setStatus(Integer.valueOf(3));
        return couponRepository.updateStatus(this);
    }

    /**
     * 更新状态
     */
    public int updateStatusOccupy() {
        this.setStatus(Integer.valueOf(2));
        return couponRepository.updateStatus(this);
    }

    /**
     * 更新状态
     * @param userId
     * @return
     */
    public int updateOneUserRecord(Integer userId) {
        String couponNumber = this.getCouponNumber();
        CouponRecord record = couponRepository.findRecord(couponNumber, userId);
        return couponRepository.updateCouponRecord(record);
    }
}
